package com.example.Administrator.shoujiweishi.activities;

import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.Administrator.shoujiweishi.R;
import com.example.Administrator.shoujiweishi.activities.base.BaseActivity;
import com.example.Administrator.shoujiweishi.fragments.SelectContacts;
import com.example.Administrator.shoujiweishi.fragments.ShowAllcontacts;
import com.example.Administrator.shoujiweishi.utils.MyUtil;
import com.example.Administrator.shoujiweishi.vo.Contact;

import java.util.ArrayList;
import java.util.List;

public class Insert_contacts extends BaseActivity {
    ShowAllcontacts frameLayout;
    SelectContacts selectContacts;
    public static TextView textView;
    public static EditText insertcontacts;
    private boolean isClose;
    List<Contact> list;
    List<Contact> selectlist=new ArrayList<Contact>();

    @Override
    public int getContentXML() {
        return R.layout.activity_insert_contacts;
    }

    @Override
    public void initData() {

    }

    boolean a=true;
    @Override
    public void initView() {
        list= MyUtil.getAllContact();
        Toolbar toolbar= (Toolbar) findViewById(R.id.tool_contact_insert);
        toolbar.setNavigationIcon(R.mipmap.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        textView= (TextView) findViewById(R.id.chancenum);
        insertcontacts= (EditText) findViewById(R.id.insertcontacts);
        if (frameLayout==null){
            frameLayout=new ShowAllcontacts();
        }

        getFragmentManager().beginTransaction().replace(R.id.framecontact,frameLayout).addToBackStack("contact").commit();
        insertcontacts.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (selectContacts==null){
                selectContacts=new SelectContacts();
                }
                if (a){
                    getFragmentManager().beginTransaction().hide(frameLayout).replace(R.id.framecontact,selectContacts).addToBackStack("contact").commit();
                    a=false;
                }

            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                selectlist.clear();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for(int j=0;j<list.size();j++){
                            Contact contact=list.get(j);
                            if(contact.getName().contains(s.toString())||contact.getPhonenum().toString().replace(" ","").replace("-","").contains(s.toString())){
                                //产生一个新的集合  true 匹配上了  false没有匹配的电话号码或联系人姓名
                                selectlist.add(contact);
                            }

                        }
                        //循环完毕之后 更新第二个碎片
                        // selectContactFragment.getSelectContactList(selectlist);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                selectContacts.getSelectContactList(selectlist);
                            }
                        });
                    }
                }).start();


            }

            @Override
            public void afterTextChanged(Editable s) {//删除完毕'

                if (insertcontacts.getText().length()==0){
                    if (getFragmentManager().getBackStackEntryCount()>1){
                        getFragmentManager().popBackStack();
                        a=true;
                    }
                }else{
                    getFragmentManager().beginTransaction().replace(R.id.framecontact,selectContacts).addToBackStack("contact").commit();
                }

            }
        });
    }

    @Override
    public void onBackPressed() {//处理回退的异常
        super.onBackPressed();
        if (getFragmentManager().getBackStackEntryCount()>1){
            getFragmentManager().popBackStack();
        }else{
            if (isClose) {
                finish();
            } else {
                int backStackCount = getFragmentManager().getBackStackEntryCount();
                for (int i = 0; i < backStackCount; i++) {
                    getFragmentManager().popBackStack();
                }
                isClose = true;
            }
        }
    }
}
